//
//  KRCateViewCell.m
//  cookBook
//
//  Created by kuroken on 16/1/29.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#define MAS_SHORTHAND

#import "KRCateViewCell.h"
#import <PureLayout.h>
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "KRCateList.h"
#import "KRCateTags.h"
#import "KRConst.h"

// cell重用标识
static NSString * const KRCateListID = @"cateList";

static NSInteger const KRMaxRow = 2;
static NSInteger const KRMaxCol = 3;
static CGFloat const MINFLOAT = 1 / MAXFLOAT;

@interface KRCateViewCell ()

/** 左边控件容器 UIView */
@property (nonatomic, weak) UIView *cateView;
/** 图片 imageView */
@property (nonatomic, weak) UIImageView *cateImageView;
/** 分类名 label */
@property (nonatomic, weak) UILabel *cateLabel;
/** 指示器 imageView */
@property (nonatomic, weak) UIImageView *indicatorImageView;
/** 内含6个tagBtn UIView */
@property (nonatomic, weak) UIView *tagsView;
/** 内含6个tagBtn */
@property (nonatomic, strong) NSMutableArray<UIButton *> *tagLabelArray;

@end

@implementation KRCateViewCell

- (NSMutableArray<UIButton *> *)tagLabelArray
{
    if (!_tagLabelArray) {
        self.tagLabelArray = [NSMutableArray array];
    }
    return _tagLabelArray;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRCateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KRCateListID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRCateListID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}


- (void)setCateList:(KRCateList *)cateList
{
    _cateList = cateList;
    
    [self.cateImageView sd_setImageWithURL:[NSURL URLWithString:cateList.imgUrl]];
    [self.cateLabel setText:cateList.cate];
    for (NSInteger i = 0; i < KRMaxRow * KRMaxCol; i++) {
        [self.tagLabelArray[i] setTitle:cateList.cateTagArray[i].tagName forState:UIControlStateNormal];
        [self.tagLabelArray[i] setTag:[cateList.cateTagArray[i].tagID integerValue]];
    }
}

/**
 *  初始化子控件
 */
- (void)setupSubview
{
    /** 左边控件容器 UIView */
    UIView *cateView = [[UIView alloc] init];
//    [cateView setBackgroundColor:[UIColor redColor]];
    [self.contentView addSubview:cateView];
    self.cateView = cateView;
   
    /** 图片 imageView */
    UIImageView *cateImageView = [[UIImageView alloc] init];
    [self.cateView addSubview:cateImageView];
    self.cateImageView = cateImageView;

    /** 分类名 label */
    UILabel *cateLabel = [[UILabel alloc] init];
    [cateLabel setFont:[UIFont systemFontOfSize:16]];
    [self.cateView addSubview:cateLabel];
    self.cateLabel = cateLabel;

    /** 指示器 imageView */
    UIImageView *indicatorImageView = [[UIImageView alloc] init];
    [indicatorImageView setImage:[UIImage imageNamed:@"ico_arrow_right"]];
    [self.cateView addSubview:indicatorImageView];
    self.indicatorImageView = indicatorImageView;

    /** 内含6个tag UIView */
    UIView *tagsView = [[UIView alloc] init];
//    tagsView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:tagsView];
    self.tagsView = tagsView;

    /** 添加6个tagLabel */
    for (NSInteger i = 0; i < 6; i++) {
        UIButton *tagBtn = [[UIButton alloc] init];
        [tagBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [tagBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [tagBtn setBackgroundColor:KRColorRGB(240)];
        [self addTargetWithBtn:tagBtn];
        [self.tagsView addSubview:tagBtn];
        [self.tagLabelArray addObject:tagBtn];
    }
}

- (void)addTargetWithBtn:(UIButton *)btn
{
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(setBtnHighlightedColor:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside];
    [btn addTarget:self action:@selector(setBtnNormalColor:) forControlEvents:UIControlEventTouchUpInside |UIControlEventTouchDragOutside | UIControlEventTouchCancel];
}

- (void)setBtnHighlightedColor:(UIButton *)btn
{
    [btn setBackgroundColor:[UIColor grayColor]];
}

- (void)setBtnNormalColor:(UIButton *)btn
{
    [btn setBackgroundColor:KRColorRGB(240)];
}


- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(cateViewCellDidClickCateBtnTagID:tagName:)]) {
        [self.delegate cateViewCellDidClickCateBtnTagID:btn.tag tagName:btn.currentTitle];
    }
}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.cateView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.cateView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.cateView autoSetDimensionsToSize:CGSizeMake(120, 70)];
    
    [self.cateImageView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.cateImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.cateImageView autoSetDimensionsToSize:CGSizeMake(45, 45)];
    
    [self.cateLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [self.cateLabel autoAlignAxis:ALAxisVertical toSameAxisOfView:self.cateImageView];
    
    [self.indicatorImageView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.indicatorImageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:5];
    [self.indicatorImageView autoSetDimensionsToSize:self.indicatorImageView.image.size];
    
    [self.tagsView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:18];
    [self.tagsView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.tagsView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.cateView withOffset:5];
    [self.tagsView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    
    NSInteger padding = 3;
    for (NSInteger i = 0; i < KRMaxRow * KRMaxCol; i++) {
        NSInteger row = i / KRMaxCol; // 行号
        NSInteger col = i % KRMaxCol; // 列号
        
        [self.tagLabelArray[i] makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tagsView.bottom).with.multipliedBy(1.0 * row / KRMaxRow + MINFLOAT).with.offset(padding);
            make.left.equalTo(self.tagsView.right).with.multipliedBy(1.0 * col / KRMaxCol + MINFLOAT).with.offset(padding);
            make.height.equalTo(self.tagsView).with.multipliedBy(1.0 / KRMaxRow).with.offset(-padding * 2);
            make.width.equalTo(self.tagsView).with.multipliedBy(1.0 / KRMaxCol).with.offset(-padding * 2);
        }];

        /* 
        [self.tagLabelArray[i] autoConstrainAttribute:ALAttributeLeft toAttribute:ALAttributeRight ofView:self.tagsView withMultiplier:1.0 * col / KRMaxCol + MINFLOAT];
        [self.tagLabelArray[i] autoConstrainAttribute:ALAttributeTop toAttribute:ALAttributeBottom ofView:self.tagsView withMultiplier:1.0 * row / KRMaxRow + MINFLOAT];
        [self.tagLabelArray[i] autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.tagsView withMultiplier:1.0 / KRMaxRow];
        [self.tagLabelArray[i] autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.tagsView withMultiplier:1.0 / KRMaxCol];
         */
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    
    [self setBackgroundColor:highlighted ? KRColorRGB(208) : [UIColor clearColor]];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    [self setBackgroundColor:selected ? KRColorRGB(208) : [UIColor clearColor]];
    
}

@end
